package geometry;

import org.lwjgl.opengl.*;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;

public class glCube 
{
	/* Translation */
	private float x;
	private float y;
	private float z;
	
	/* Rotation */
	private float rotX;
	private float rotY;
	private float rotZ;
	
	/* Dimensions */
	private float side;
	
	/* Edges */
	private boolean seeEdges;
	
	/* Texture */
	Texture texture = null;
	
	/* When created, angles are all set to 0 degrees */
	public glCube(float x, float y, float z, float s, boolean edges)
	{
		this.x = x;
		this.y = y;
		this.z = z;
		this.side = s;
		
		this.seeEdges = edges;
	}
	
	public void setTexture(Texture texture)
	{
		this.texture = texture;
		System.out.println(texture.getTextureID());
	}
	
	public void move(float velX, float velY, float velZ, float dt)
	{
		this.x += velX*dt;
		this.y += velY*dt;
		this.z += velZ*dt;
		
		System.out.println("x (" + this.x + "). y (" + this.y + "). z (" + this.z + ")" );
	}
	
	public void setVisibleEdges(boolean val)
	{
		this.seeEdges = val;
	}
	
	/* Rotate by a certain amount */
	public void rotate(float rotX, float rotY, float rotZ)
	{
		this.rotX += rotX;
		this.rotY += rotY;
		this.rotZ += rotZ;
	}
	
	/* The cube is drawn from the lower left corner.  */
	public void drawCube()
	{
		GL11.glPushMatrix(); /* Save matrix state */
		
		/* Coordinates of the cube */
		GL11.glTranslatef(x, y, z);
		
		/* Rotate the cube on its local axis */
		GL11.glRotatef(rotX, 1.0f, 0, 0);
		GL11.glRotatef(rotY, 0, 1.0f, 0);
		GL11.glRotatef(rotZ, 0, 0, 1.0f);
		
		/* Draw edges */
		if(seeEdges)
		{
			/* Draw edges */
			GL11.glColor3f(0f, 0, 0);
			GL11.glBegin(GL11.GL_LINES);
			{
				/* Top */
				drawLine(side/2, side/2, side/2, /* Front */
						-side/2, side/2, side/2);  
				drawLine(-side/2, side/2, side/2, /* Left */
						 -side/2, side/2, -side/2);
				drawLine(-side/2, side/2, -side/2, /* Back */
						  side/2, side/2, -side/2);
				drawLine(side/2, side/2, -side/2, /* Right */
						 side/2, side/2, side/2);	
				
				/* Sides */
	            drawLine(-side/2, -side/2, side/2,
	            		 -side/2, side/2, side/2);
	            drawLine(side/2, -side/2, side/2,
	           		     side/2, side/2, side/2);
	            drawLine(-side/2, -side/2, -side/2,
	           		 	 -side/2, side/2, -side/2);
	            drawLine(side/2, -side/2, -side/2,
	           		 	 side/2, side/2, -side/2);
	            
	            /* Bottom */
	            drawLine(side/2, -side/2, side/2, /* Front */
						-side/2, -side/2, side/2);  
				drawLine(-side/2, -side/2, side/2, /* Left */
						 -side/2, -side/2, -side/2);
				drawLine(-side/2, -side/2, -side/2, /* Back */
						  side/2, -side/2, -side/2);
				drawLine(side/2, -side/2, -side/2, /* Right */
						 side/2, -side/2, side/2);
			}
			GL11.glEnd();
		}
		
		if(texture == null)
		{
			GL11.glDisable(GL11.GL_TEXTURE_2D);
			GL11.glEnable(GL11.GL_COLOR_MATERIAL); 
			GL11.glColor3f(1.0f, 1.0f, 1.0f);
		}
		else
		{
			//GL11.glEnable(GL11.GL_TEXTURE_2D);
			//GL11.glDisable(GL11.GL_COLOR_MATERIAL); 
			//Color.white.bind();
			//GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID()); // or simply "texture.bind();"
		}
		
		GL11.glBegin(GL11.GL_QUADS);
		{
			GL11.glNormal3f(0, 1.0f, 0);
			GL11.glVertex3f( side/2, side/2, -side/2); 		  // Top Right Of The Quad (Top)
			GL11.glVertex3f(-side/2, side/2,-side/2);         // Top Left Of The Quad (Top)
			GL11.glVertex3f(-side/2, side/2, side/2);         // Bottom Left Of The Quad (Top)
			GL11.glVertex3f( side/2, side/2, side/2);         // Bottom Right Of The Quad (Top)
            
			GL11.glNormal3f(0, -1.0f, 0);
			GL11.glVertex3f( side/2,-side/2, side/2);         // Top Right Of The Quad (Bottom)
			GL11.glVertex3f(-side/2,-side/2, side/2);         // Top Left Of The Quad (Bottom)
			GL11.glVertex3f(-side/2,-side/2,-side/2);         // Bottom Left Of The Quad (Bottom)
			GL11.glVertex3f( side/2,-side/2,-side/2);         // Bottom Right Of The Quad (Bottom)
          
			GL11.glNormal3f(0, 0, 1.0f);
			GL11.glVertex3f( side/2, side/2, side/2);         // Top Right Of The Quad (Front)
			GL11.glVertex3f(-side/2, side/2, side/2);         // Top Left Of The Quad (Front)
			GL11.glVertex3f(-side/2,-side/2, side/2);         // Bottom Left Of The Quad (Front)
			GL11.glVertex3f( side/2,-side/2, side/2);         // Bottom Right Of The Quad (Front)
			
			GL11.glNormal3f(0, 0, -1.0f);
            GL11.glVertex3f( side/2,-side/2,-side/2);         // Bottom Left Of The Quad (Back)
            GL11.glVertex3f(-side/2,-side/2,-side/2);         // Bottom Right Of The Quad (Back)
            GL11.glVertex3f(-side/2, side/2,-side/2);         // Top Right Of The Quad (Back)
            GL11.glVertex3f( side/2, side/2,-side/2);         // Top Left Of The Quad (Back)
          
            GL11.glNormal3f(-1.0f, 0, 0);
            GL11.glVertex3f(-side/2, side/2, side/2);         // Top Right Of The Quad (Left)
            GL11.glVertex3f(-side/2, side/2,-side/2);         // Top Left Of The Quad (Left)
            GL11.glVertex3f(-side/2,-side/2,-side/2);         // Bottom Left Of The Quad (Left)
            GL11.glVertex3f(-side/2,-side/2, side/2);         // Bottom Right Of The Quad (Left)
          
            GL11.glNormal3f(1.0f, 0, 0);
            GL11.glVertex3f( side/2, side/2,-side/2);         // Top Right Of The Quad (Right)
            GL11.glVertex3f( side/2, side/2, side/2);         // Top Left Of The Quad (Right)
            GL11.glVertex3f( side/2,-side/2, side/2);         // Bottom Left Of The Quad (Right)
            GL11.glVertex3f( side/2,-side/2,-side/2);         // Bottom Right Of The Quad (Right)
		}
		GL11.glEnd();
		GL11.glPopMatrix();
		
		/* Disable textures again */
		if(texture != null)
			GL11.glDisable(GL11.GL_TEXTURE_2D);
	}
	
	public void drawFace()
	{
		
	}
	
	public void drawLine(float x1, float y1, float z1, 
			             float x2, float y2, float z2)
	{
		GL11.glVertex3f(x1, y1, z1);
        GL11.glVertex3f(x2, y2, z2);
	}
	
	public boolean isCollidingWith(glCube other)
	{
		return false;
	}
	
	/*public Bounds getBounds()
	{
		
	}
	*/
}
